package com.vedeng.study.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description:
 *
 * @author Thresh.xie
 * @date 2021/12/18
 */

@Configuration
public class TopicRabbitConfig {
    /**  绑定键  */
    private final static String MAN = "topic.man";
    /**  绑定键  */
    private final static String WOMAN= "topic.woman";
    /**  绑定键  */
    private final static String ALL= "topic";

    @Bean
    public Queue firstQueue() {
        return new Queue(MAN);
    }
    @Bean
    public Queue secondQueue() {
        return new Queue(WOMAN);
    }
    @Bean
    public Queue thirdQueue() {
        return new Queue(ALL);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }

    /**  将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
     *   这样消息携带的路由键是topic.man,才会分发到该队列
     */
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(MAN);
    }
    /** 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.woman
     *  这样消息携带的路由键是topic.woman,都会分发到该队列
     */
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with(WOMAN);
    }
    /** 将 thirdQueue 和 topicExchange 绑定,而且绑定的键值为用上通配路由键规则 topic.#
     *  这样只要是消息携带的路由键是以 topic. 开头,都会分发到该队列
     */
    @Bean
    Binding bindingExchangeMessage3() { return BindingBuilder.bind(thirdQueue()).to(exchange()).with("topic.#"); }

}
